﻿
using Azure.AI.Inference;
using ConsoleAI.Vector;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;

List<CloudService> cloudServices =
[
    new() {
            Key = 0,
            Name = "Azure App Service",
            Description = "Host .NET, Java, Node.js, and Python web applications and APIs in a fully managed Azure service. You only need to deploy your code to Azure. Azure takes care of all the infrastructure management like high availability, load balancing, and autoscaling."
    },
    new() {
            Key = 1,
            Name = "Azure Service Bus",
            Description = "A fully managed enterprise message broker supporting both point to point and publish-subscribe integrations. It's ideal for building decoupled applications, queue-based load leveling, or facilitating communication between microservices."
    },
    new() {
            Key = 2,
            Name = "Azure Blob Storage",
            Description = "Azure Blob Storage allows your applications to store and retrieve files in the cloud. Azure Storage is highly scalable to store massive amounts of data and data is stored redundantly to ensure high availability."
    },
    new() {
            Key = 3,
            Name = "Microsoft Entra ID",
            Description = "Manage user identities and control access to your apps, data, and resources."
    },
    new() {
            Key = 4,
            Name = "Azure Key Vault",
            Description = "Store and access application secrets like connection strings and API keys in an encrypted vault with restricted access to make sure your secrets and your application aren't compromised."
    },
    new() {
            Key = 5,
            Name = "Azure AI Search",
            Description = "Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization."
    }
];
//创建一个向量存储库
var vectorStore = new InMemoryVectorStore();

//从向量存储库里面拿电影集合
var services = vectorStore.GetCollection<int, CloudService>("cloudService");
//如果拿的向量不存在，则创建一个
await services.CreateCollectionIfNotExistsAsync();

IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator = new EmbeddingsClient(new Uri("https://models.inference.ai.azure.com"), new Azure.AzureKeyCredential("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")).AsIEmbeddingGenerator("text-embedding-3-large");

foreach (var service in cloudServices)
{
    //通过嵌入式生成器生成向量并赋值给service对象的vector属性
    service.Vector=await embeddingGenerator.GenerateEmbeddingVectorAsync(service.Description);
    //将已经添加过向量属性的对象上传至向量存储库
    await services.UpsertAsync(service);
}
//创建一个用来检索的文本
string query = "Which Azure service should I use to store my Word documents?";

var queryVector= await embeddingGenerator.GenerateEmbeddingVectorAsync(query);

//创建一个搜索选项

VectorSearchOptions<CloudService> vectorSearchOptions=new VectorSearchOptions<CloudService>()
{
    Top=2,
    VectorProperty=s=>s.Vector,
};

var results=await services.VectorizedSearchAsync(queryVector, vectorSearchOptions);

await foreach (var result in results.Results)
{
    Console.WriteLine(result.Record.Name);
}

Console.ReadKey();